{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8f3ab24a",
   "metadata": {},
   "source": [
    "# 1. requests(让HTTPS服务人类)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cd57131a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20fb0431",
   "metadata": {},
   "source": [
    "**r 是指 respond**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "13468b69",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"https://cn.bing.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "86b888f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c22c72eb",
   "metadata": {},
   "source": [
    "**r.content**\n",
    "1. 可以获取请求数据内容（观察页面数据形态HTML,Json）\n",
    "2. b 是指bytes：二进制文件\n",
    "**r.json**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "577d99ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"http://httpbin.org/get\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "26d7e0bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3e6dfd13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.25.1',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62306f0f-09117b6b2d12eb2b0bc134e3'},\n",
       " 'origin': '120.85.41.39',\n",
       " 'url': 'http://httpbin.org/get'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()  # 讲json数据转换成字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a38f1664",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'120.85.41.39'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['origin']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16180dcc",
   "metadata": {},
   "source": [
    "**payload**\n",
    "使用payload来实现请求数据传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c9bf3162",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'name':'kaijun',\n",
    "    'identity':'student'\n",
    "}\n",
    "\n",
    "r = requests.get(\"https://httpbin.org/get\", params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5acd6f7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "25e7f6f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'identity': 'student', 'name': 'kaijun'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.25.1',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62306f10-4e5f6b6d07838e0f50be47a3'},\n",
       " 'origin': '120.85.41.39',\n",
       " 'url': 'https://httpbin.org/get?name=kaijun&identity=student'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56e1d19d",
   "metadata": {},
   "source": [
    "# 2. face++ detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "24fb70d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 准备base url （ 对应该功能的url链接）\n",
    "face_detect_url= \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 2. 准备参数数据\n",
    "payload ={\n",
    "    'api_key':'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy',\n",
    "    'api_secret':'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT',\n",
    "    'image_url':'https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/8694a4c27d1ed21b2b25b386ab6eddc450da3fe0.jpg',\n",
    "    'return_landmark':1,\n",
    "    'return_attributes':'gender,age,smiling,emotion'\n",
    "}\n",
    "# 3. 检查调用的请求方法：POST\n",
    "# 4. 请求人脸数据\n",
    "r = requests.post(url = face_detect_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fb051e2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9ea654e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647341411,49148a17-ceaa-4fa9-9097-968444d4030e',\n",
       " 'time_used': 337,\n",
       " 'faces': [{'face_token': '2d4c93467a4a6125cfbdcfe03902ed61',\n",
       "   'face_rectangle': {'top': 246, 'left': 175, 'width': 420, 'height': 420},\n",
       "   'landmark': {'contour_chin': {'x': 404, 'y': 676},\n",
       "    'contour_left1': {'x': 222, 'y': 298},\n",
       "    'contour_left2': {'x': 207, 'y': 349},\n",
       "    'contour_left3': {'x': 199, 'y': 401},\n",
       "    'contour_left4': {'x': 199, 'y': 455},\n",
       "    'contour_left5': {'x': 209, 'y': 506},\n",
       "    'contour_left6': {'x': 230, 'y': 554},\n",
       "    'contour_left7': {'x': 261, 'y': 598},\n",
       "    'contour_left8': {'x': 300, 'y': 636},\n",
       "    'contour_left9': {'x': 348, 'y': 664},\n",
       "    'contour_right1': {'x': 598, 'y': 372},\n",
       "    'contour_right2': {'x': 595, 'y': 412},\n",
       "    'contour_right3': {'x': 589, 'y': 453},\n",
       "    'contour_right4': {'x': 573, 'y': 492},\n",
       "    'contour_right5': {'x': 550, 'y': 528},\n",
       "    'contour_right6': {'x': 527, 'y': 562},\n",
       "    'contour_right7': {'x': 500, 'y': 595},\n",
       "    'contour_right8': {'x': 473, 'y': 627},\n",
       "    'contour_right9': {'x': 445, 'y': 659},\n",
       "    'left_eye_bottom': {'x': 399, 'y': 339},\n",
       "    'left_eye_center': {'x': 403, 'y': 326},\n",
       "    'left_eye_left_corner': {'x': 362, 'y': 315},\n",
       "    'left_eye_lower_left_quarter': {'x': 378, 'y': 329},\n",
       "    'left_eye_lower_right_quarter': {'x': 422, 'y': 342},\n",
       "    'left_eye_pupil': {'x': 392, 'y': 319},\n",
       "    'left_eye_right_corner': {'x': 442, 'y': 343},\n",
       "    'left_eye_top': {'x': 410, 'y': 307},\n",
       "    'left_eye_upper_left_quarter': {'x': 385, 'y': 306},\n",
       "    'left_eye_upper_right_quarter': {'x': 432, 'y': 320},\n",
       "    'left_eyebrow_left_corner': {'x': 332, 'y': 260},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 371, 'y': 260},\n",
       "    'left_eyebrow_lower_middle': {'x': 408, 'y': 267},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 443, 'y': 283},\n",
       "    'left_eyebrow_right_corner': {'x': 480, 'y': 293},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 372, 'y': 244},\n",
       "    'left_eyebrow_upper_middle': {'x': 414, 'y': 248},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 452, 'y': 263},\n",
       "    'mouth_left_corner': {'x': 357, 'y': 528},\n",
       "    'mouth_lower_lip_bottom': {'x': 438, 'y': 593},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 404, 'y': 549},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 376, 'y': 557},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 402, 'y': 580},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 471, 'y': 561},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 479, 'y': 574},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 463, 'y': 589},\n",
       "    'mouth_lower_lip_top': {'x': 448, 'y': 562},\n",
       "    'mouth_right_corner': {'x': 491, 'y': 555},\n",
       "    'mouth_upper_lip_bottom': {'x': 452, 'y': 557},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 444, 'y': 533},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 403, 'y': 531},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 407, 'y': 545},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 475, 'y': 540},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 487, 'y': 546},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 473, 'y': 558},\n",
       "    'mouth_upper_lip_top': {'x': 459, 'y': 541},\n",
       "    'nose_contour_left1': {'x': 464, 'y': 348},\n",
       "    'nose_contour_left2': {'x': 438, 'y': 433},\n",
       "    'nose_contour_left3': {'x': 445, 'y': 485},\n",
       "    'nose_contour_lower_middle': {'x': 475, 'y': 497},\n",
       "    'nose_contour_right1': {'x': 513, 'y': 360},\n",
       "    'nose_contour_right2': {'x': 515, 'y': 447},\n",
       "    'nose_contour_right3': {'x': 499, 'y': 493},\n",
       "    'nose_left': {'x': 418, 'y': 466},\n",
       "    'nose_right': {'x': 518, 'y': 482},\n",
       "    'nose_tip': {'x': 495, 'y': 468},\n",
       "    'right_eye_bottom': {'x': 562, 'y': 378},\n",
       "    'right_eye_center': {'x': 561, 'y': 363},\n",
       "    'right_eye_left_corner': {'x': 525, 'y': 363},\n",
       "    'right_eye_lower_left_quarter': {'x': 542, 'y': 371},\n",
       "    'right_eye_lower_right_quarter': {'x': 579, 'y': 376},\n",
       "    'right_eye_pupil': {'x': 549, 'y': 356},\n",
       "    'right_eye_right_corner': {'x': 590, 'y': 368},\n",
       "    'right_eye_top': {'x': 567, 'y': 341},\n",
       "    'right_eye_upper_left_quarter': {'x': 543, 'y': 346},\n",
       "    'right_eye_upper_right_quarter': {'x': 584, 'y': 350},\n",
       "    'right_eyebrow_left_corner': {'x': 548, 'y': 314},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 568, 'y': 313},\n",
       "    'right_eyebrow_lower_middle': {'x': 588, 'y': 311},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 605, 'y': 314},\n",
       "    'right_eyebrow_right_corner': {'x': 619, 'y': 320},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 566, 'y': 295},\n",
       "    'right_eyebrow_upper_middle': {'x': 590, 'y': 290},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 611, 'y': 298}},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 40},\n",
       "    'smile': {'value': 54.035, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.003,\n",
       "     'disgust': 13.356,\n",
       "     'fear': 0.003,\n",
       "     'happiness': 86.236,\n",
       "     'neutral': 0.393,\n",
       "     'sadness': 0.004,\n",
       "     'surprise': 0.004}}}],\n",
       " 'image_id': '/CIZ9JMaRl8Szp7Ni5nwCw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa89f2b5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
